package com.meirimoney.business;

import android.content.Context;
import android.content.SharedPreferences;

import com.meirimoney.sdk.models.AccessToken;

/***
 * 用户信息类
 * 
 * @author gewei
 * 
 */
public class UserInformation {
	private final static UserInformation instance = new UserInformation();

	/***
	 * 获取存储区的Key
	 * 
	 * @return
	 */
	private String getKey() {
		return "user";
	}

	/**
	 * 获取当前TokenManage
	 */
	public static UserInformation getInstance() {
		return instance;
	}

	/***
	 * 获取资源访问Token
	 * 
	 * @param context
	 *            当前上下文
	 * @return
	 */
	public AccessToken getAccessToken(Context context) {
		AccessToken at = null;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				this.getKey(), Context.MODE_PRIVATE);
		String token = sharedPreferences.getString("token", null);
		String secret = sharedPreferences.getString("secret", null);
		int integralBalance = sharedPreferences.getInt("integralBalance", 0);
		if (token != null && secret != null) {
			at = new AccessToken();
			at.Token = token;
			at.Secret = secret;
			at.IntegralBalance = integralBalance;
		}
		return at;
	}

	/***
	 * 保存AccessToken
	 * 
	 * @param context
	 *            当前上下文
	 * @param accessToken
	 */
	public void saveAccessToken(Context context, AccessToken accessToken) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				this.getKey(), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString("token", accessToken.Token);
		editor.putString("secret", accessToken.Secret);
		editor.putInt("integralBalance", accessToken.IntegralBalance);
		editor.commit();
	}

	/**
	 * 保存积分余额
	 * 
	 * @param integralBalance
	 *            积分余额
	 */
	public void saveIntegralBalance(Context context, int integralBalance) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				this.getKey(), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putInt("integralBalance", integralBalance);
		editor.commit();
	}

	/**
	 * 获取积分余额
	 * 
	 * @param context
	 *            当前执行的上下文
	 * @return 返回积分余额
	 */
	public int getIntegralBalance(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				this.getKey(), Context.MODE_PRIVATE);
		return sharedPreferences.getInt("integralBalance", 0);
	}
}
